const App = getApp();
import { url } from '../../modelApi/url_config.js'
Page({
	data: {
		statusBarHeight: "",
		statusBarPadding: "",
		hasuserInfo: false, //获取用户信息
		grade: 0,
		isAdvince: false,
		maskHidden: false,
		imagePath: '',
		showShareBool: false,
		sharetype: '',
		qrcodeImg: '',
		bannerTemp: '', //本地海报
		downloadQrctemp: '', //二维码本地图
		avatartempImg: '', //本地头像
	},
	onLoad: function (options) {
		this.setData({
			statusBarPadding: App.globalData.navTop,
			statusBarHeight: App.globalData.navHeight,
			grade: wx.getStorageSync('grade') || 0,
			isAdvince: options.type ? true : false
		});
		this.data.isAdvince && this.downloadavatar()
		// 判断是否用户授权
		wx.getSetting({
			success: (res) => {
				this.setData({
					hasuserInfo: res.authSetting['scope.userInfo'] && res.errMsg === "getSetting:ok"
				})
			}
		})
	},
	onShareAppMessage(res) {
		const shareUserId = wx.getStorageSync('isMember') ? wx.getStorageSync('userId') : (wx.getStorageSync('shareUserId') || '')
		const url_data = `/pages/upgradeList/sonpages/detail/index?upgrade_type=${this.data.sharetype}&phoneNumber=${wx.getStorageSync('tel')}&shareUserId=${shareUserId}`;
		const titile = +this.data.sharetype === 4 ? '邀请您成为轮到你播主播' : +this.data.sharetype === 5 ? '邀请您成为轮到你播经纪人' : '邀请您成为轮到你播董事'
		const imgUrl = +this.data.sharetype === 4 ? 'https://lundao01.oss-cn-beijing.aliyuncs.com/wxapp/static/img/element_poster_01.jpg' :
			+this.data.sharetype === 5 ?  'https://lundao01.oss-cn-beijing.aliyuncs.com/wxapp/static/img/element_poster_02.jpg'
			: 'https://lundao01.oss-cn-beijing.aliyuncs.com/wxapp/static/img/element_poster_03.jpg'
		return {
			title: wx.getStorageSync('nickName') + titile,
			path: url_data,
			imageUrl: imgUrl
		}
	},
	/* 跳转升级详情页面 */
	goUpgradeDetail(event) {
		if (this.data.isAdvince) {
			this.setData({
				sharetype: event.currentTarget.dataset.type
			})
			this.setData({
				showShareBool: true
			})
			this.getQrcode()
			return
		}
		if (+event.currentTarget.dataset.type === 4 && ~~this.data.grade >= 3) {
			return
		}
		if (+event.currentTarget.dataset.type === 5 && ~~this.data.grade >= 4) {
			return
		}
		if (+event.currentTarget.dataset.type === 6 && ~~this.data.grade >= 5) {
			return
		}
		wx.navigateTo({
			url: "/pages/upgradeList/sonpages/detail/index?upgrade_type=" + event.currentTarget.dataset.type
		});
	},
	cancelImgFunc() {
		this.setData({
			maskHidden: false
		})
	},
	saveImgFunc() {
		wx.saveImageToPhotosAlbum({
			filePath: this.data.imagePath,
			success(res) { }
		})
	},
	closeShareFunc() {
		this.setData({
			showShareBool: false
		})
	},
	getQrcode() {
		let shareUserId = null
		if (wx.getStorageSync('isMember')) {
				shareUserId = wx.getStorageSync('userId');
		} else {
				shareUserId = wx.getStorageSync('shareUserId') || '';
		}
		const tel = wx.getStorageSync('tel');
		const params = {
			page: 'pages/upgradeList/sonpages/detail/index',
			scene: `${this.data.sharetype}&${tel}&${shareUserId}`,
			isHyaline: true,
			width: '280'
		}
		App.ajax(url.getQrcode, params, 'post').then(res => {
			this.setData({
				qrcodeImg: res.data
			})
			this.downloadQrc()
			this.downloadtempbannerImg()
		})
	},
	creatBanner() {
		var that = this;
		this.setData({
			maskHidden: false,
			showShareBool: false
		});
		wx.showToast({
			title: '图片生成中...',
			icon: 'loading',
			duration: 500
		});
		setTimeout(function () {
			that.createNewImg();
			that.setData({
				maskHidden: true
			});
		}, 500)
	},
	downloadavatar() {
		const that = this;
		wx.downloadFile({
			url: wx.getStorageSync('avatar'),
			success(res) {
				if (res.statusCode === 200) {
					that.setData({
						avatartempImg:res.tempFilePath
					})
				}
			}
		})
	},
	downloadQrc() {
			const that = this;
			// 小程序码
			wx.downloadFile({
				url: that.data.qrcodeImg,
				success(qrcodeImgOBJ) {
					if (qrcodeImgOBJ.statusCode === 200) {
						that.setData({
							downloadQrctemp: qrcodeImgOBJ.tempFilePath
						})
					}
				}
			})
	},
	downloadtempbannerImg() {
		const that = this;
		const imgUrl = +this.data.sharetype === 4 ? 'https://lundao01.oss-cn-beijing.aliyuncs.com/wxapp/static/img/element_poster_01.jpg' :
			+this.data.sharetype === 5 ?  'https://lundao01.oss-cn-beijing.aliyuncs.com/wxapp/static/img/element_poster_02.jpg'
			: 'https://lundao01.oss-cn-beijing.aliyuncs.com/wxapp/static/img/element_poster_03.jpg'
			// 主图海报
		wx.downloadFile({
			url: imgUrl,
			success(res) {
				if (res.statusCode === 200) {
					that.setData({
						bannerTemp: res.tempFilePath
					})
				}
			}
		})
	},
	createNewImg: function () {
		const that = this;
		const context = wx.createCanvasContext('mycanvas');
		// 背景
		context.setFillStyle('#fff')
		context.fillRect(0, 0, 400, 750)
		// 主图海报
		context.drawImage(this.data.bannerTemp, 0, 0, 400, 544);
		// 绘制名字
		context.setFontSize(14);
		context.setFillStyle('#333333');
		context.fillText(wx.getStorageSync('nickName'), 74, 688);
		context.stroke();
		// 长按识别
		context.setFontSize(16);
		context.setFillStyle('#999999');
		context.fillText('长按图片识别开通', 256, 720);
		context.stroke();
		// 小程序码
		context.drawImage(this.data.downloadQrctemp, 252, 560, 128, 128);
		// 头像
		context.save()
		context.beginPath()
		context.arc(40, 684, 20, 0, 2 * Math.PI)
		context.clip();
		context.drawImage(this.data.avatartempImg, 20, 664, 40, 40);
		context.restore() // 恢复画布
		context.draw();
		//将生成好的图片保存到本地，需要延迟一会，绘制期间耗时
		setTimeout(function () {
			wx.canvasToTempFilePath({
				canvasId: 'mycanvas',
				success: function (res) {
					wx.hideToast()
					var tempFilePath = res.tempFilePath;
					that.setData({
						imagePath: tempFilePath,
						canvasHidden: true
					});
				},
				fail: function (res) {
					console.log(res);
				}
			});
		},500);
	},
})